package dyyx;

import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;

import dyyx.entity.User;
import dyyx.mapper.UserMapper;
import dyyx.util.CommUtil;

public class Main {

	public static void main(String[] args) throws Exception {

		SqlSessionFactory sqlSessionFactory = buildSqlSessionFactory();

		boolean autocommit = true;
		// 获取 SqlSession ， 封装了jdbc connection
		SqlSession session = sqlSessionFactory.openSession(autocommit);

		System.out.println("session=" + session);
		System.out.println("autocommit=" + autocommit);
		System.out.println("session.conn.autoCommit=" + session.getConnection().getAutoCommit());

		// init 创建表 insert 初始数据
		String sqlstr = CommUtil.readFromResource("db/data.sql", null);
		System.out.println(sqlstr);
		String[] sqls = StringUtils.split(sqlstr, ";");
		for (String sql : sqls) {
			if (StringUtils.isBlank(sql)) {
				continue;
			}
			// 获取 jdbc 连接 执行 初始化语句
			session.getConnection().createStatement().executeUpdate(sql);
		}
		
		UserMapper mapper = session.getMapper(UserMapper.class);
		
		User user = mapper.selectById(1L);
		System.out.println("user=" + user);
		
		user = new User();
		user.setId(3L);
		user.setName_("dragon");
		user.setBirth(new Date());
		user.setHeight(100);
		user.setWeight(66.66);
		user.setUserInfo("hello");
		
		mapper.insert(user);
		user = mapper.selectById(user.getId());
		System.out.println("user=" + user);
		
		User userNew = new User();
		userNew.setId(user.getId());
		userNew.setWeight(66.88);
		userNew.setUserInfo("hello2");

		
		mapper.updateById(userNew);
		
		user = mapper.selectById(user.getId());
		System.out.println("user=" + user);
		
		List<User> users = mapper.selectList(null);
		System.out.println(users);

		
		
		
		

		
		
		

	}

	public static SqlSessionFactory buildSqlSessionFactory() {
		DataSource dataSource = dataSource();
		TransactionFactory transactionFactory = new JdbcTransactionFactory();
		Environment environment = new Environment("dev", transactionFactory, dataSource);
		MybatisConfiguration configuration = new MybatisConfiguration(environment);
		configuration.setLogImpl(StdOutImpl.class);
		configuration.addMapper(UserMapper.class);
		return new MybatisSqlSessionFactoryBuilder().build(configuration);
	}

	public static DataSource dataSource() {
		// 连接池
		PooledDataSource dataSource = new PooledDataSource();
		dataSource.setDriver("org.h2.Driver");
		dataSource.setUsername("sa");
		dataSource.setPassword("");
		dataSource.setUrl("jdbc:h2:mem:testdb");

		return dataSource;
	}

}
